#define DEBUG
#include <cstdio>

using namespace std;

typedef long long ll;
const ll maxn=10000, maxm=1000, inf=0x7fffffffffffff;


ll min(ll a, ll b) {
	return a<b ? a : b;
}

class Graph {
	public:
		ll n, m, ind[maxn+3], to[2*maxn+6*maxm+1], val[2*maxn+6*maxm+1], link[2*maxn+6*maxm+1], pri[2*maxn+6*maxm+1];

		void init(ll n) {
			this->n = n, m = 1;
			for (ll i=1; i<=n; i++) {
				ind[i] = 0;
			}
		}

		void addSide(ll u, ll v, ll w, ll p=0) {
			m++;
			to[m] = v;
			val[m] = w;
			pri[m] = p;
			link[m] = ind[u];
			ind[u] = m;
		}
};

class Network : public Graph {
	public:
		const ll s=1, t=2;
		ll dis[maxn+3], cur[maxn+3];

		void spfa() {
			static bool book[maxn+1];
			static ll que[maxn+1];
			for (ll i=1; i<=n; i++) {
				dis[i] = inf;
			}
			book[s] = true, dis[s] = 0, que[1] = s;
			for (ll h=0, t=1; h!=t; ) {
				h = h%n+1;
				ll x=que[h];
				book[x] = false;
				for (ll i=ind[x]; i; i=link[i]) {
					if (val[i]) {
						ll y=to[i];
						if (dis[y]>dis[x]+pri[i]) {
							dis[y] = dis[x]+pri[i];
							if (!book[y]) {
								t = t%n+1;
								que[t] = y;
								book[y] = true;
							}
						}
					}
				}
			}
		}

		ll run(ll o, ll flow, ll &money) {
			static bool book[maxn+1];
			book[o] = true;
			if (o==t) {
				book[o] = false;
				return flow;
			}
			ll have=0;
			for (ll i=cur[o]; i; i=link[i]) {
				cur[o] = i;
				if (val[i] && dis[to[i]] == dis[o]+pri[i] && !book[to[i]]) {
					ll t = run(to[i], min(flow-have, val[i]), money);
					have += t, val[i]-=t, val[i^1]+=t, money+=t*pri[i];
					if (have==flow) {
						book[o] = false;
						return have;
					}
				}
			}
			book[o] = false;
			return have;
		}

		ll solve() {
			ll ret=0;
			for (spfa(); dis[t]<inf; spfa()) {
				for (ll i=1; i<=n; i++) {
					cur[i] = ind[i];
				}
				run(s, inf, ret);
			}
			return ret;
		}
};

class Tree : public Graph {
	public:
		ll dep[maxn+1];

		void build(ll o, ll fa, Network &t) {
			dep[o] = dep[fa]+1;
			for (ll i=ind[o]; i; i=link[i]) {
				if (to[i]!=fa) {
					t.addSide(to[i]+2, o+2, val[i], 0);
					t.addSide(o+2, to[i]+2, 0, 0);
					build(to[i], o, t);
				}
			}
		}
};

int main() {
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);

	static Tree tree;
	static Network net;
	ll t;
	scanf("%lld", &t);
	for (ll ti=1; ti<=t; ti++) {
		ll n, m;
		scanf("%lld %lld", &n, &m);
		tree.init(n), net.init(n+2);
		for (ll i=1; i<n; i++) {
			ll x, y, d;
			scanf("%lld %lld %lld", &x, &y, &d);
			tree.addSide(x, y, d);
			tree.addSide(y, x, d);
		}
		tree.build(1, 0, net);
		ll sum=0;
		for (ll i=1; i<=m; i++) {
			ll u, v, c;
			scanf("%lld %lld %lld", &u, &v, &c);
			sum += c;
			if (tree.dep[u]<tree.dep[v]) {
				ll t;
				t = u, u = v, v = t;
			}
			net.addSide(1, u+2, 1, 0);
			net.addSide(u+2, 1, 0, 0);
			net.addSide(v+2, 2, 1, 0);
			net.addSide(2, v+2, 0, 0);
			net.addSide(u+2, v+2, 1, c);
			net.addSide(v+2, u+2, 0, -c);
		}
		printf("%lld\n", sum-net.solve());
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}
